/*     */ package com.zimbra.cs.service.admin;
/*     */ 
/*     */ import com.zimbra.common.account.Key.ServerBy;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.AdminConstants;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Server;
/*     */ import com.zimbra.cs.account.accesscontrol.AdminRight;
/*     */ import com.zimbra.cs.account.accesscontrol.Rights.Admin;
/*     */ import com.zimbra.cs.rmgmt.RemoteMailQueue;
/*     */ import com.zimbra.cs.rmgmt.RemoteMailQueue.QueueAttr;
/*     */ import com.zimbra.cs.rmgmt.RemoteMailQueue.SearchResult;
/*     */ import com.zimbra.cs.rmgmt.RemoteMailQueue.SummaryItem;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import java.util.Collections;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import org.apache.lucene.index.Term;
/*     */ import org.apache.lucene.search.BooleanClause.Occur;
/*     */ import org.apache.lucene.search.BooleanQuery;
/*     */ import org.apache.lucene.search.Query;
/*     */ import org.apache.lucene.search.TermQuery;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class GetMailQueue
/*     */   extends AdminDocumentHandler
/*     */ {
/*     */   public static final int MAIL_QUEUE_QUERY_DEFAULT_LIMIT = 30;
/*     */   public static final int MAIL_QUEUE_SCAN_DEFUALT_WAIT_SECONDS = 3;
/*     */   public static final int MAIL_QUEUE_SUMMARY_CUTOFF = 100;
/*     */   
/*     */   public Element handle(Element request, Map<String, Object> context)
/*     */     throws ServiceException
/*     */   {
/*  51 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*  52 */     Provisioning prov = Provisioning.getInstance();
/*     */     
/*  54 */     Element serverElem = request.getElement("server");
/*  55 */     String serverName = serverElem.getAttribute("name");
/*     */     
/*  57 */     Server server = prov.get(Key.ServerBy.name, serverName);
/*  58 */     if (server == null) {
/*  59 */       throw ServiceException.INVALID_REQUEST("server with name " + serverName + " could not be found", null);
/*     */     }
/*     */     
/*  62 */     checkRight(zsc, context, server, Rights.Admin.R_manageMailQueue);
/*     */     
/*  64 */     Element queueElem = serverElem.getElement("queue");
/*  65 */     String queueName = queueElem.getAttribute("name");
/*  66 */     boolean scan = queueElem.getAttributeBool("scan", false);
/*  67 */     long waitMillis = queueElem.getAttributeLong("wait", 3L) * 1000L;
/*     */     
/*  69 */     Element queryElem = queueElem.getElement("query");
/*  70 */     int offset = (int)queryElem.getAttributeLong("offset", 0L);
/*  71 */     int limit = (int)queryElem.getAttributeLong("limit", 30L);
/*  72 */     Query query = buildLuceneQuery(queryElem);
/*     */     
/*  74 */     RemoteMailQueue rmq = RemoteMailQueue.getRemoteMailQueue(server, queueName, scan);
/*  75 */     boolean stillScanning = rmq.waitForScan(waitMillis);
/*  76 */     RemoteMailQueue.SearchResult sr = rmq.search(query, offset, limit);
/*     */     
/*  78 */     Element response = zsc.createElement(AdminConstants.GET_MAIL_QUEUE_RESPONSE);
/*  79 */     serverElem = response.addElement("server");
/*  80 */     serverElem.addAttribute("name", serverName);
/*     */     
/*  82 */     queueElem = serverElem.addElement("queue");
/*  83 */     queueElem.addAttribute("name", queueName);
/*  84 */     queueElem.addAttribute("time", rmq.getScanTime());
/*  85 */     queueElem.addAttribute("scan", stillScanning);
/*  86 */     queueElem.addAttribute("total", rmq.getNumMessages());
/*  87 */     queueElem.addAttribute("more", offset + limit < sr.hits);
/*     */     
/*  89 */     for (RemoteMailQueue.QueueAttr attr : sr.sitems.keySet()) {
/*  90 */       List<RemoteMailQueue.SummaryItem> slist = (List)sr.sitems.get(attr);
/*  91 */       Collections.sort(slist);
/*  92 */       qsElem = queueElem.addElement("qs");
/*  93 */       qsElem.addAttribute("type", attr.toString());
/*  94 */       i = 0;
/*  95 */       for (RemoteMailQueue.SummaryItem sitem : slist) {
/*  96 */         i++;
/*  97 */         if (i > 100) {
/*     */           break;
/*     */         }
/* 100 */         Element qsiElem = qsElem.addElement("qsi");
/* 101 */         qsiElem.addAttribute("n", sitem.count());
/* 102 */         qsiElem.addAttribute("t", sitem.term());
/*     */       } }
/*     */     Element qsElem;
/*     */     int i;
/* 106 */     for (Iterator i$ = sr.qitems.iterator(); i$.hasNext();) { qitem = (Map)i$.next();
/* 107 */       qiElem = queueElem.addElement("qi");
/* 108 */       for (RemoteMailQueue.QueueAttr attr : qitem.keySet())
/* 109 */         qiElem.addAttribute(attr.toString(), (String)qitem.get(attr)); }
/*     */     Map<RemoteMailQueue.QueueAttr, String> qitem;
/*     */     Element qiElem;
/* 112 */     return response;
/*     */   }
/*     */   
/*     */   public static Query buildLuceneQuery(Element queryElem) throws ServiceException {
/* 116 */     BooleanQuery fq = new BooleanQuery();
/* 117 */     boolean emptyQuery = true;
/* 118 */     for (Iterator fieldIter = queryElem.elementIterator("field"); fieldIter.hasNext();) {
/* 119 */       emptyQuery = false;
/* 120 */       Element fieldElement = (Element)fieldIter.next();
/* 121 */       String fieldName = fieldElement.getAttribute("name");
/* 122 */       BooleanQuery mq = new BooleanQuery();
/* 123 */       for (Iterator matchIter = fieldElement.elementIterator("match"); matchIter.hasNext();) {
/* 124 */         Element matchElement = (Element)matchIter.next();
/* 125 */         String matchValue = matchElement.getAttribute("value");
/* 126 */         Term term = new Term(fieldName, matchValue);
/* 127 */         mq.add(new TermQuery(term), BooleanClause.Occur.SHOULD);
/*     */       }
/* 129 */       fq.add(mq, BooleanClause.Occur.MUST);
/*     */     }
/* 131 */     if (emptyQuery) {
/* 132 */       return null;
/*     */     }
/* 134 */     return fq;
/*     */   }
/*     */   
/*     */ 
/*     */   public void docRights(List<AdminRight> relatedRights, List<String> notes)
/*     */   {
/* 140 */     relatedRights.add(Rights.Admin.R_manageMailQueue);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/admin/GetMailQueue.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */